﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace MathQ
{
    public partial class MainForm : Form
    {

        private List<MathEquaCtrl> eqLst;
        private bool isCompleted = false;
        private List<Image> _helloImages;
        private List<Image> _winImages;

        public MainForm()
        {
            InitializeComponent();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            Icon ic = (Icon)Program._resources.GetObject("poolbird");
            this.Icon = ic;

            _helloImages = new List<Image>();
            _helloImages.Add((Image)Program._resources.GetObject("poolballpng"));
            _helloImages.Add((Image)Program._resources.GetObject("poolbirdpng"));
            _helloImages.Add((Image)Program._resources.GetObject("poolhatpng"));
            _helloImages.Add((Image)Program._resources.GetObject("poolleafpng"));
            _helloImages.Add((Image)Program._resources.GetObject("poolsnowmanpng"));

            _winImages = new List<Image>();
            _winImages.Add((Image)Program._resources.GetObject("gif001"));
            _winImages.Add((Image)Program._resources.GetObject("gif002"));
            _winImages.Add((Image)Program._resources.GetObject("gif003"));
            _winImages.Add((Image)Program._resources.GetObject("gif004"));
            _winImages.Add((Image)Program._resources.GetObject("gif005"));
            _winImages.Add((Image)Program._resources.GetObject("gif006"));
            _winImages.Add((Image)Program._resources.GetObject("gif007"));
            _winImages.Add((Image)Program._resources.GetObject("gif008"));
            _winImages.Add((Image)Program._resources.GetObject("gif009"));
            _winImages.Add((Image)Program._resources.GetObject("gif010"));
            _winImages.Add((Image)Program._resources.GetObject("gif011"));
            _winImages.Add((Image)Program._resources.GetObject("gif012"));
            _winImages.Add((Image)Program._resources.GetObject("gif013"));
            _winImages.Add((Image)Program._resources.GetObject("gif014"));
            _winImages.Add((Image)Program._resources.GetObject("gif015"));
            _winImages.Add((Image)Program._resources.GetObject("gif016"));
            _winImages.Add((Image)Program._resources.GetObject("gif017"));
            _winImages.Add((Image)Program._resources.GetObject("gif018"));

            Program._rnd = new Random();
            pictureBox_hello.Image = _helloImages[Program._rnd.Next(0, 5)];

            PopulateNewMathEquaList();            

            eqLst = new List<MathEquaCtrl>();
            for (int x = 0; x <= 3; x++)
            {
                for (int y = 0; y <= 4; y++)
                {
                    MathEquaCtrl eq = new MathEquaCtrl(Program._mathEquaList[x * 5 + y]);
                    this.Controls.Add(eq);
                    eq.Left = 40 + x * eq.Width;
                    eq.Top = 160 + y * 90;
                    eqLst.Add(eq);
                }
            }

            scoreItems1.Visible = false;
            toolStripButton_chk.Enabled = false;
            toolStripButton_finish.Enabled = false;
            toolStripButton_crct.Enabled = false;

        }


        private void PopulateNewMathEquaList()
        {
            Program._mathEquaList = new List<MathEqua>();
            for (int i = 0; Program._mathEquaList.Count < 20; i++)
            {
                MathEqua m = new MathEqua();
                //if (!Program._mathEquaList.Contains(m))
                //{
                //    Program._mathEquaList.Add(m);
                //}
                bool isDup = false;
                foreach (MathEqua mq in Program._mathEquaList)
                {
                    if (m.X == mq.X && m.Y == mq.Y && m.Opt == mq.Opt)
                    {
                        isDup = true;
                        break;
                    }
                }

                if (!isDup) Program._mathEquaList.Add(m);
            }
        }


        private void toolStripButton_chk_Click(object sender, EventArgs e)
        {
            if (isCompleted)
            {
                if (toolStripButton_chk.Text == "打分")
                {
                    int wrongTotal = 0;
                    foreach (MathEquaCtrl eq in eqLst)
                    {
                        if (eq.CheckResult() == false)
                        {
                            wrongTotal++;
                        }
                    }
                    scoreItems1.DisplayScore(eqLst.Count, wrongTotal);
                    scoreItems1.Visible = true;
                    toolStripButton_chk.Enabled = false;

                    if (scoreItems1.Score == 100)
                    {
                        label_hello.Text = "Excellent!";
                        toolStripButton_restart.Enabled = true;




                    }
                    else
                    {
                        toolStripButton_crct.Enabled = true;
                        if (scoreItems1.IsGoodScore)
                        {
                            label_hello.Text = "Well done!";
                            pictureBox_hello.Image = _winImages[Program._rnd.Next(0, 18)];

                        }
                    }
                }
                else //检查
                {
                    int wrongTotal = 0;
                    foreach (MathEquaCtrl eq in eqLst)
                    {
                        if (eq.IsEnabled)
                        {
                            if (eq.CheckResult() == false) wrongTotal++;
                            else eq.DisableAnswCombo();
                        }
                    }

                    if (wrongTotal == 0)
                    {
                        toolStripButton_chk.Enabled = false;
                        toolStripButton_restart.Enabled = true;
                    }
                }
            }
        }


        private void toolStripButton_start_Click(object sender, EventArgs e)
        {
            scoreItems1.Visible = false;
            toolStripButton_restart.Enabled = false;
            toolStripButton_chk.Enabled = false;
            toolStripButton_finish.Enabled = true;
            toolStripButton_start.Enabled = false;
            toolStripButton_crct.Enabled = false;

            foreach (MathEquaCtrl eq in eqLst)
            {
                eq.EnableAnswCombo();
            }
            clock1.Start();
        }

        private void toolStripButton_finish_Click(object sender, EventArgs e)
        {
            isCompleted = true;
            foreach (MathEquaCtrl eq in eqLst)
            {
                if (!eq.HasValue)
                {
                    MessageBox.Show("题还没有做完！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isCompleted = false;
                    break;
                }
            }

            if (isCompleted)
            {
                clock1.Stop();
                toolStripButton_chk.Enabled = true;
                foreach (MathEquaCtrl eq in eqLst)
                {
                    eq.DisableAnswCombo();
                }
                toolStripButton_finish.Enabled = false;
                toolStripButton_restart.Enabled = false;
                toolStripButton_chk.Enabled = true;
            }

            return;
        }


        private void toolStripButton_restart_Click(object sender, EventArgs e)
        {
            //foreach (MathEquaCtrl eq in eqLst)
            //{
            //    eq.InitEquation();
            //}
            PopulateNewMathEquaList(); 
            for (int i = 0; i < eqLst.Count; i++)
            {
                eqLst[i].MathEqua = Program._mathEquaList[i];
            }

            scoreItems1.Visible = false;
            toolStripButton_start.Enabled = true;
            toolStripButton_chk.Enabled = false;
            toolStripButton_chk.Text = "打分";
            toolStripButton_crct.Enabled = false;
            clock1.Reset();
            pictureBox_hello.Image = _helloImages[Program._rnd.Next(0, 5)];
            label_hello.Text = "Hello Linda!";
        }


        private void pictureBox_hello_Click(object sender, EventArgs e)
        {
            if (scoreItems1.Visible && scoreItems1.IsGoodScore)
            {
                if (scoreItems1.Score == 100)
                {
                    pictureBox_hello.Image = _winImages[Program._rnd.Next(0, 18)];
                }
            }
            else
            {
                pictureBox_hello.Image = this._helloImages[Program._rnd.Next(0, 5)];
            }
        }


        //改错
        private void toolStripButton_crct_Click(object sender, EventArgs e)
        {
            foreach (MathEquaCtrl eq in eqLst)
            {
                if (eq.CheckResult() == false)
                {
                    eq.EnableAnswCombo();
                }
            }

            //scoreItems1.Visible = true;
            toolStripButton_chk.Text = "检查";
            toolStripButton_chk.Enabled = true;
            toolStripButton_crct.Enabled = false;
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isCompleted && !toolStripButton_restart.Enabled && 
                MessageBox.Show("题还没有做完，确定要退出吗？", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != 
                DialogResult.Yes)
            {
                e.Cancel = true;
            }

        }




    }
}
